const cloud = require('wx-server-sdk');
const queryOrderId = require("../queryOrderId/index");
const updateUser = require("../updateUser/index");
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});
const db = cloud.database();
// const _ = db.command;
exports.main = async (event, context) => {
  if (!event.openId) {
    return {
      code: 500,
      msg: "缺少openId"
    }
  }
  const getOrderRes = await queryOrderId.main(event, context);
  if (getOrderRes.code !== 200) {
    return getOrderRes
  }
  try {
    const resList = await Promise.all([updateUser.main({
      ...event.createUser,
      openId: event.openId
    }, context), db.collection('userOrders').add({
      data: {
        orderId: event.orderId,
        customerData: event.customerData,
        sellerData: event.sellerData,
        openId: event.openId,
        lockInfo: event.lockInfo,
        createUser: event.createUser,
        imgs: event.imgs,
        createTime: event.inTime,
        dbTime: event.dbTime,
        success: "0"
      }
    })]);
    return {
      ...resList[1],
      code: 200
    };
  } catch (error) {
    console.error(error)
    return {
      code: 500,
      msg: "服务器内部异常"
    }
  }


};